//
//  ViewController.m
//  构架模式MVC
//
//  Created by XHJ on 2020/8/24.
//  Copyright © 2020 L. All rights reserved.
//

#import "ViewController.h"
#import "LDataSource.h"
#import "DataModel.h"
#import "TableViewCell.h"

static NSString *const identifier = @"iden";

@interface ViewController () <UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)LDataSource *dataSource;
@property (nonatomic, strong)DataModel *model;
@property (nonatomic, strong)UIView *customView;
@end

@implementation ViewController

//- (void)loadView {
//    self.view = self.customView;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataSource = [[LDataSource alloc] initWithCellIdentifier:identifier configure:^(TableViewCell *cell, DataModel *model, NSIndexPath * _Nonnull indexPath) {
        cell.model = model;
        cell.textLabel.text = model.name;
        cell.detailTextLabel.text = model.subName;
    }];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self.dataSource;
    [DataModel loadDataSuccess:^(id  _Nonnull data) {
        [self.dataSource addDataArr:data];
        [self.tableView reloadData];
    } failure:^{
        
    }];
    //  如果需要改某个cell数据最好加个安全锁
    @synchronized (self) {
        
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.rowHeight = 40;
        [_tableView registerClass:[TableViewCell class] forCellReuseIdentifier:identifier];
    }
    return _tableView;
}

@end

